//本程序实现功能为：按下按键一选择LED亮灯方式；按下按键二可切换流水灯模式
		//知识点***按键消抖***，switch语句  if语句for循环  按位或&
#include <reg52.h>
#include <intrins.h>
sbit Key1 = P3^1;
sbit Key2 = P3^0;

void Delay(xms)		//@11.0592MHz	 延时一毫秒函数
{
	unsigned char i, j;
  while(xms--)
	{
	_nop_();
	i = 2;
	j = 199;
	do
	{
		while (--j);
	} while (--i);
}
}


void main()
{
	unsigned char i,a;
	unsigned char mode=1;
	unsigned int kind;
	while(1)
	{
		if(mode==1)		//判读是否为实现模式一
	 {
		 kind=0;
		 while(mode==1) //若是模式一 一直循环
		{
			
		 switch(kind)
		 {
			 //if(Key1==0){Delay(20);if(Key1==0)while(!Key1);Delay(20);} 更好的消抖
			 case 0:P2=0xe7;if(Key1==0){Delay(20);if(Key1==0)while(!Key1);Delay(20);kind++;}break;
			 case 1:P2=0xc3;if(Key1==0){Delay(20);if(Key1==0)while(!Key1);Delay(20);kind++;}break;
			 case 2:P2=0x81;if(Key1==0){Delay(20);if(Key1==0)while(!Key1);Delay(20);kind++;}break;
			 case 3:P2=0x00;if(Key1==0){Delay(20);if(Key1==0)while(!Key1);Delay(20);kind++;}break;
			 case 4:P2=0x81;if(Key1==0){Delay(20);if(Key1==0)while(!Key1);Delay(20);kind++;}break;
			 case 5:P2=0xc3;if(Key1==0){Delay(20);if(Key1==0)while(!Key1);Delay(20);kind++;}break;
			 case 6:P2=0xe7;if(Key1==0){Delay(20);if(Key1==0)while(!Key1);Delay(20);kind++;}break;
			 case 7:P2=0xff;if(Key1==0){Delay(20);if(Key1==0)while(!Key1);Delay(20);kind++;}break;
			 case 8:kind=0;break;
		 }
			if(Key2==0){Delay(20);if(Key2==0)while(!Key2);Delay(20);mode = 2;}		//按下按键二 切换模式二
	 }
	}
			

	 if(mode==2)
	 {
			kind=0;
		 	while(mode==2)
		{
		 switch(kind)
		 {
			 //if(Key1==0){while(!Key1);Delay(20);} 更好的消抖
			 case 0:for(i=0;i<=7;i++){P2=~(0x01<<i);Delay(200);if(Key2==0){Delay(20);if(Key2==0)while(!Key2);Delay(20);mode=1;break;}if(Key1==0){Delay(20);if(Key1==0)while(!Key1);Delay(20);kind++;break;}}break;  //按下按键一切换流水灯方式，按下按键二切换模式二
			 case 1:for(i=0;i<=7;i++){P2=~(0x80>>i);Delay(200);if(Key2==0){Delay(20);if(Key2==0)while(!Key2);Delay(20);mode=1;break;}if(Key1==0){Delay(20);if(Key1==0)while(!Key1);Delay(20);kind++;break;}}break;
			 case 2:for(i=0;i<=3;i++){P2=~((0x10<<i)|(0x08>>i));Delay(200);if(Key2==0){Delay(20);if(Key2==0)while(!Key2);Delay(20);mode=1;break;}if(Key1==0){Delay(20);if(Key1==0)while(!Key1);Delay(20);kind++;break;}}break;
			 case 3:for(i=0;i<=3;i++){P2=~((0x01<<i)|(0x80>>i));Delay(200);if(Key2==0){Delay(20);if(Key2==0)while(!Key2);Delay(20);mode=1;break;}if(Key1==0){Delay(20);if(Key1==0)while(!Key1);Delay(20);kind++;break;}}break;
			 case 4:for(i=0;i<=3;i++){P2=~(0x03<<2*i);Delay(200);if(Key2==0){Delay(20);if(Key2==0)while(!Key2);Delay(20);mode=1;break;}if(Key1==0){Delay(20);if(Key1==0)while(!Key1);Delay(20);kind++;break;}}break;
			 case 5:for(i=0;i<=3;i++){P2=~(0xc0>>2*i);Delay(200);if(Key1==0){Delay(20);if(Key1==0)while(!Key1);Delay(20);kind++;break;}}break;
			 case 6:for(i=0;i<1;i){P2=~0xaa;Delay(200);P2=~0x55;Delay(200);if(Key2==0){Delay(20);if(Key2==0)while(!Key2);Delay(20);mode=1;break;}if(Key1==0){Delay(20);if(Key1==0)while(!Key1);Delay(20);kind++;break;}}break;
			 case 7:kind=0;break;
		 }
		 if(Key2==0){Delay(20);if(Key2==0)while(!Key2);Delay(20);mode=1;}
	  }
   }
	 
 }
}
